export default {
    buildMap(wordList) {
        var result = {};
        var count = wordList.length;
        for (var i = 0; i < count; ++i) {
            var map = result;
            var word = wordList[i];
            for (var j = 0; j < word.length; ++j) {
                var ch = word.charAt(j);
                if (typeof(map[ch]) != "undefined") {
                    map = map[ch];
                    if (map["empty"]) {
                        break;
                    }
                } else {
                    if (map["empty"]) {
                        delete map["empty"];
                    }
                    map[ch] = { "empty": true };
                    map = map[ch];
                }
            }
        }
        return result;
    },
    check(map, content) {
        console.log('map:', map);
        console.log('content:', content);
        var result = [];
        var count = content.length;
        var stack = [];
        var point = map;
        for (var i = 0; i < count; ++i) {
            var ch = content.charAt(i);
            var item = point[ch];
            if (typeof(item) == "undefined") {
                i = i - stack.length;
                stack = [];
                point = map;
            } else if (item["empty"]) {
                stack.push(ch);
                result.push(stack.join(""));
                stack = [];
                point = map;
            } else {
                stack.push(ch);
                point = item;
            }
        }
        return result;
    }
}